package com.mobile.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.service.SysRoleManager;
import com.hsnn.medstgmini.sys.service.SysUserManager;
import com.hsnn.medstgmini.util.Pagination;
import com.yspa.yspa.service.YspaInterfaceManager;

/**
 * 用户
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/mobile")
public class MobileUserSetController {

	private static final Logger log = Logger.getLogger(MobileUserSetController.class);

	@Autowired
	private SysUserManager userManager;

	@Autowired
	private HttpSession session;

	@Autowired
	private YspaInterfaceManager manager;
	
	@Autowired
	private HttpServletRequest request;

	/**
	 * @category 中心跳转用户新增编辑页面
	 * @date 2015年6月14日
	 * @return
	 */
	@RequestMapping("/toUserSet")
	public String toUserSet(Model model) {
		log.info("enter toUserSet method");
		Map<String,Object> userMap = (Map<String, Object>) session.getAttribute(Constants.USERINFO);
		if (userMap == null) {
			return "redirect:/mobile/login.html";
		}
		model.addAttribute("user", userMap);
		return "/mobile/userSet";
	}
	
	@RequestMapping("/toUserIMEI")
	public String toUserIMEI(Model model) {
		log.info("enter toUserIMEI method");
		Map<String,Object> userMap = (Map<String, Object>) session.getAttribute(Constants.USERINFO);
		if (userMap == null) {
			return "redirect:/mobile/login.html";
		}
		model.addAttribute("user", userMap);
		return "/mobile/userIMEI";
	}
	
	@RequestMapping("/saveUserIMEI")
	@ResponseBody
	public Pagination saveUserIMEI() {
		Pagination page = new Pagination();
		Map<String,Object> userMap = (Map<String, Object>) session.getAttribute(Constants.USERINFO);
		if (userMap == null) {
			page.setMsg("请重新登录！");
			 page.setCode(-99);
			page.setSuccess(false);
			return page;
		}
		try {
			String userno = userMap.get("userno").toString();
			page.getConditions().put("userno", userno);//用户号
			page.getConditions().put("imei", request.getParameter("imei"));
			page.getConditions().put("imei2", request.getParameter("imei2"));
			page.getConditions().put("imei3", request.getParameter("imei3"));
			manager.updateUserInfo(page);
			
			userMap.put("imei", request.getParameter("imei"));
			userMap.put("imei2", request.getParameter("imei2"));
			userMap.put("imei3", request.getParameter("imei3"));
			session.setAttribute(Constants.USERINFO, userMap);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("", e);
			page.setMsg("保存失败！");
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * 保存主用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("/saveUser")
	@ResponseBody
	public Pagination saveZUser(SysUser user) {
		log.info(user.toString());
		Pagination page = new Pagination();
		Map<String,Object> userMap = (Map<String, Object>) session.getAttribute(Constants.USERINFO);
		try {
//			user.setId(su.getId());
//			user.setAcctExpired(su.getAcctExpired());
//			user.setRemark(su.getRemark());
//			userManager.saveZUser(user, su.getId());
//			SysUser newuser = userManager.getUserByUsername(su.getUsername());
//			if(newuser != null){
//				session.setAttribute(Constants.USERINFO, newuser);
//				page.setSuccess(true);
//			}
		} catch (Exception e) {
			log.error("", e);
			page.setMsg("保存失败！");
			page.setSuccess(false);
		}
		return page;
	}

	@RequestMapping("/addUser")
	@ResponseBody
	public Pagination addZUser(SysUser user) {
		log.info(user.toString());
		Pagination page = new Pagination();
		if (null != user) {
			try {
				userManager.addZUser(user);
				page.setSuccess(true);
			} catch (Exception e) {
				page.setSuccess(false);
			}
		}
		return page;
	}

}
